﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace HenIT.BizTalk
{
    public class BTSDBDAL
    {
        public BTSDBDAL() { }
        public BTSDBDAL(string mgmtServer, string mgmtDb)
        {
            ConnectionString = "Server=" + mgmtServer + ";Database=" + mgmtDb + ";Trusted_Connection=True;";
        }
        public string ConnectionString { get; set; }

        public string GetBTSVersion()
        {
            string versionStr = "";
            string sql = "SELECT TOP 1 [ProductMajor],[ProductMinor],[ProductBuildNumber],[ProductRevision],[Modified] FROM [BizTalkDBVersion]";
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmnd = new SqlCommand(sql, conn))
                {
                    DataSet ds = new DataSet();
                    using (SqlDataAdapter da = new SqlDataAdapter(cmnd))
                    {
                        da.Fill(ds);
                        if (ds.Tables.Count == 1)
                        {
                            DataRow r = ds.Tables[0].Rows[0];
                            versionStr = r["ProductMajor"].ToString() + "." + 
                                    r["ProductMinor"].ToString() + "." + 
                                    r["ProductBuildNumber"].ToString() + "." + "." + 
                                    r["ProductRevision"].ToString() + " ("  + 
                                    r["Modified"].ToString() + ")";
                        }
                    }
                    
                }
            }
            return versionStr;
        }
    }
}
